﻿using CoreModule;
using Microsoft.Extensions.Logging;
using System;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
namespace CoreModule
{
    /// <summary>
    /// 扫码枪
    /// </summary>
    public class KeyboardRecognitionDevice : IRecognitionDevice
    {
        /// <summary>
        /// 
        /// </summary>
        public static string DeviceName = "扫码枪/扫码器";
        /// <summary>
        /// 
        /// </summary>
        public bool IsConnected => true;
  
        ILogger Logger { get; set; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="DeviceManager"></param>
        public KeyboardRecognitionDevice(ILogger logger)
        {
            Logger = logger;
        }
        /// <summary>
        /// 
        /// </summary>
        public event EventHandler<RecognitionResultInfoEventArgs> RecognitionResultInfoCompleted;
        /// <summary>
        /// 
        /// </summary>
        public event EventHandler<DeviceStatusEventArgs> DeviceStatusChanged;
        /// <summary>
        /// 
        /// </summary>
        public void StartDevice()
        {
            Application.Current.MainWindow.PreviewKeyUp += MainWindow_PreviewKeyUp;
            Logger.LogInformation("KeyboardRecognitionDevice StartDevice");
        }
        /// <summary>
        /// 
        /// </summary>
        public StringBuilder Cache { get; set; } = new StringBuilder();
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainWindow_PreviewKeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            
           var enterKey = (char)KeyInterop.VirtualKeyFromKey(Key.Enter);
            var returnKey = (char)KeyInterop.VirtualKeyFromKey(Key.Return);
            var cancelKey = (char)KeyInterop.VirtualKeyFromKey(Key.Cancel);
            var capitalKey = (char)KeyInterop.VirtualKeyFromKey(Key.Capital);
            var capsLockKey = (char)KeyInterop.VirtualKeyFromKey(Key.CapsLock);
            char inputChar = (char)KeyInterop.VirtualKeyFromKey(e.Key); // 将按键转换为字符
            if (inputChar!= enterKey 
                &&  inputChar!= returnKey
                && inputChar != cancelKey
                && inputChar != capitalKey
                && inputChar != capsLockKey)
            {
                Cache.Append(inputChar);
            }
            if (e.Key == Key.Enter && Cache.Length>0)
            {
                string input = Cache.ToString().Trim();
                var parts = input.Split(new char[] { enterKey, returnKey, cancelKey, capitalKey, capsLockKey }, StringSplitOptions.RemoveEmptyEntries)
                    .Distinct();
                Cache.Clear();
                foreach (var part in parts)
                {
                    e.Handled = true;
                      
                        RecognitionResultInfoCompleted?.Invoke(this, new RecognitionResultInfoEventArgs()
                        {
                           
                            RecognitionResultInfo = new RecognitionResultInfo()
                            {
                                IsChanged=true,
                                IsEmpty=false,
                                RecognitionSourceKing = RecognitionSourceKing.Keyboard,
                                KeyboardInput = part,
                                PatientInfo = new PatientInfo()
                                {
                                    WdCode=part,
                                }
                            }
                        });
//                    DeviceStatusChanged?.Invoke(this, new DeviceStatusEventArgs() { DeviceStatus = DeviceStatus.Connected });
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public void StopDevice()
        {
            if (Application.Current.MainWindow != null)
            {
                Application.Current.MainWindow.PreviewKeyUp -= MainWindow_PreviewKeyUp;
                Logger.LogInformation("KeyboardRecognitionDevice StopDevice");
            }
    
        }
       
    }
}
